<?php
include 'functions.php';
//Connect to the database
mysql_connect("localhost","tftpot_ryan","Kl14ein86");
mysql_select_db("tftpot_tftp");
$parameters = json_decode(file_get_contents("php://input"));
//If they are authenticated query is run and JSONObject of thoughts is returned
if(authenticated($parameters)==true){
	$counter = 0;
	$stallNumberQuery = mysql_query("select * from tftpot_tftp.Stalls where Stalls.name = '".$parameters -> {'stallName'}."'");
	while($stall=mysql_fetch_array($stallNumberQuery)){
		$stallNumber = $stall['stall'];
	}
	$q=mysql_query("select * from tftpot_tftp.Thoughts where Thoughts.stall = ".$stallNumber." order by timestamp desc limit ".$parameters -> {'numberOfThoughts'});
	while($e=mysql_fetch_assoc($q)){
		$output[]=$e;
		$output[$counter]['timeStamp'] = date('M-d-Y h:i',getTimeStamp($output[$counter]['timeStamp']));
		$counter = $counter + 1;
	}
	$result = array('success' => 'true', 'thoughts' => $output);
	print(json_encode($result));
	mysql_close();
}
else{
	$result = array('success' => 'false', 'error'=>'error');
	print(json_encode($result));
	mysql_close();
}
//Create a human readable time stamp
function getTimeStamp($mySQLTime){
	$year = substr($mySQLTime, 0,4);
	$month = substr($mySQLTime, 5,2);
	$day = substr($mySQLTime, 8,2);
	$hour = substr($mySQLTime, 11,2);
	$minute = substr($mySQLTime, 14,2);
	$second = substr($mySQLTime, 17,2);
	$unixTime = mktime($hour+3, $minute, $second, $month, $day, $year);
	return $unixTime;
}

?>
